﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using wf = System.Windows.Forms;
using WPF_DbCenter.CommonMethod;

namespace WPF_DbCenter.SqlCE.UI
{
    /// <summary>
    /// Interaction logic for ResetData.xaml
    /// </summary>
    public partial class ResetData : Window
    {
        public ResetData()
        {
            InitializeComponent();
            ShowInTaskbar = false;
        }

        private void butSelect_Click(object sender, RoutedEventArgs e)
        {
            using (wf.OpenFileDialog of = new System.Windows.Forms.OpenFileDialog())
            {
                of.Filter = "SqlCe Db File(*.sdf)|*.sdf|All Files(*.*)|*.*";
                if (of.ShowDialog() == wf.DialogResult.OK)
                {
                    textBox1.Text = of.FileName;
                }
            }
        }

        private void but_ResetData_Click(object sender, RoutedEventArgs e)
        {
            string db = textBox1.Text;
            if (string.IsNullOrEmpty(db))
            {
                MessageBox.Show("DB name is not valid");
                return;
            }
            string pwd = passwordBox1.Password;
            CoreEA.ICoreEAHander h = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlCE35).X_Handler;
            try
            {
                h.Open(new CoreEA.LoginDbInfo(db, pwd, (bool)checkBox1.IsChecked));
                foreach (string table in h.GetTableListInDatabase(db))
                {
                    h.RemoveAllData(table);
                }
                h.Close();
                MessageBox.Show("Reset data Completed");
            }
            catch (Exception eee)
            {
                XLCS.Common.ProcessException.DisplayErrors(eee);
            }
        }

        private void but_ResetCurrent_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                CoreEA.ICoreEAHander h = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlCE35).X_Handler;
                h.Open(new CoreEA.LoginDbInfo(
                    ((CEEnginer)App.MainEngineer).CurOpeningDbName,
((CEEnginer)App.MainEngineer).CurPwd,
                true)
                );

                foreach (string table in ((CEEnginer)App.MainEngineer).TableList)
                {
                    h.RemoveAllData(table);
                }

                h.Close();

                MessageBox.Show("Reset data Completed");
            }
            catch (Exception eee)
            {
                XLCS.Common.ProcessException.DisplayErrors(eee);
            }
        }
    }
}
